import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import 'package:hj_crm/entity/conference_info.dart';
import 'package:hj_crm/entity/distributors/distributor_assist.dart';
import 'package:hj_crm/entity/distributors/distributor_conferences.dart';
import 'package:hj_crm/entity/distributors/distributor_info.dart';
import 'package:hj_crm/entity/distributors/distributor_orders.dart';
import 'package:hj_crm/entity/distributors/distributor_outbound.dart';
import 'package:hj_crm/entity/login_entity.dart';
import 'package:hj_crm/entity/order_info.dart';
import 'package:hj_crm/entity/outbound_info.dart';
import 'package:hj_crm/entity/pagination.dart';
import 'package:hj_crm/entity/perms_menu.dart';
import 'package:hj_crm/entity/user_info.dart';

import 'http_request.dart';
import 'request.dart';
import 'request_api.dart';
import 'request_result_platform.dart';

const int pageSize = 20;

class RequestRepository {
  /// 获取图形验证码
  getCaptcha({
    int width = 110,
    int height = 36,
    Success<dynamic>? success,
    Fail? fail,
  }) {
    Request.appGet(
      RequestApi.captcha,
      {'width': width, 'height': height},
      dialog: false,
      token: false,
      // isLogin: true,
      isJson: true,
      success: (data) {
        success?.call(data);
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 发送验证码
  sendCode({
    String? phone,
    Success<RequestResultPlatform>? success,
    Fail? fail,
  }) {
    Request.appGet(
      RequestApi.smsCode,
      {'mobile': phone},
      dialog: false,
      isJson: true,
      success: (data) {
        var result = RequestResultPlatform.fromJson(data as dynamic);
        success?.call(result);
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  ///登录请求
  /// [account]账号
  /// [password]密码
  /// [success] 请求成功回调
  /// [fail] 请求失败回调
  login({
    String? username,
    String? password,
    String? captchaId,
    String? verifyCode,
    Success<LoginEntity>? success,
    Fail? fail,
  }) {
    // const appMid = "1378618860613144576";

    Map<String, dynamic> params = {
      'grant_type': 'password',
      'username': username,
      'password': password,
      'captchaId': captchaId,
      'verifyCode': verifyCode,
      'login_type': 'password',
    };

    Request.appPost(
      RequestApi.login,
      params,
      token: true,
      isJson: true,
      isLogin: true,
      loginType: 'password',
      success: (data) {
        var loginInfo = LoginEntity.fromJson(data as Map<String, dynamic>);
        success?.call(loginInfo);
      },
      fail: (code, msg) {
        debugPrint('object error : $msg');
        fail?.call(code, msg);
      },
    );
  }

  /// 获取应用权限
  getPermissions({
    Success<dynamic>? success,
    Fail? fail,
  }) {
    Request.appGet(
      RequestApi.permissions,
      null,
      dialog: false,
      isJson: true,
      success: (data) {
        var permsMenu = PermsMenu.fromJson(data as Map<String, dynamic>);
        success?.call(permsMenu);
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 图片上传
  imageUpload({
    required Uint8List bytesData,
    required String fileName,
    Success? success,
    Fail? fail,
  }) {
    MultipartFile file = MultipartFile.fromBytes(bytesData, filename: fileName);
    FormData formData = FormData.fromMap({
      'files': [file]
    });

    Request.appPost(RequestApi.fileUpload, formData, dialog: false, isJson: true, success: (data) {
      success?.call(data!);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

//=======================================
  /// 用户列表
  userList({
    int page = 1,
    int size = pageSize,
    bool sortDesc = true,
    String name = '',
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
    };
    if (name.isNotEmpty) {
      paramMap['name'] = name;
    }
    Request.appGet(
      RequestApi.userList,
      paramMap,
      dialog: false,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<UserInfo>.from(resultData['list'].map((e) => UserInfo.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增用户
  addUser({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addUser, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 获取用户详情
  findUserInfo({
    required String id,
    Success<UserInfo>? success,
    Fail? fail,
  }) {
    Request.appGet(
      RequestApi.findUser,
      <String, dynamic>{'id': id},
      dialog: false,
      isJson: true,
      success: (data) {
        success?.call(UserInfo.fromJson(data as Map<String, dynamic>));
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 修改用户
  updateUser({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateUser, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除用户
  deleteUser({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteUser, ids, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //====================================================

  /// 查询订单列表
  findOrderList({
    int page = 1,
    int size = pageSize,
    String uId = '',
    bool sortDesc = true,
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
      "userInfoId": uId,
    };
    Request.appGet(
      RequestApi.findOrderList,
      paramMap,
      dialog: false,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<OrderInfo>.from(resultData['list'].map((e) => OrderInfo.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增订单
  addOrder({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addOrder, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改订单
  updateOrder({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateOrder, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除订单
  deleteOrder({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteOrder, ids, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //================================================================
  /// 查询会议记录
  findConferenceList({
    int page = 1,
    int size = pageSize,
    bool sortDesc = true,
    // 降序排列
    String order = "createdAt",
    String uId = '',
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
      "userInfoId": uId,
    };
    Request.appGet(
      RequestApi.conferenceList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<ConferenceInfo>.from(
                resultData['list'].map((e) => ConferenceInfo.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增参会记录
  addConference({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addConference, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改会议记录
  updateConference({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateConference, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除会议记录
  deleteConference({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteConference, ids, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //================================================================
  /// 查询出库记录
  findOutboundList({
    int page = 1,
    int size = pageSize,
    String uId = '',
    bool sortDesc = true,
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "userInfoId": uId,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
    };
    Request.appGet(
      RequestApi.outboundList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<OutboundInfo>.from(resultData['list'].map((e) => OutboundInfo.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增出库记录
  addOutbound({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addOutbound, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改出库记录
  updateOutbound({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateOutbound, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除出库记录
  deleteOutbound({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteOutbound, ids, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// =================================================================================================
  /// =================================================================================================
  /// =================================================================================================

  /// 经销商列表
  distributorList({
    int page = 1,
    int size = pageSize,
    bool sortDesc = true,
    String name = '',
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
    };
    if (name.isNotEmpty) {
      paramMap['customerName'] = name;
    }
    Request.appGet(
      RequestApi.distributorList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;

          success(PaginatedData(
            data: List<DistributorInfo>.from(
                resultData['list'].map((e) => DistributorInfo.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 获取用户详情
  findDistributorInfo({
    required String id,
    Success<DistributorInfo>? success,
    Fail? fail,
  }) {
    Request.appGet(
      RequestApi.findDistributor,
      <String, dynamic>{'id': id},
      dialog: false,
      isJson: true,
      success: (data) {
        success?.call(DistributorInfo.fromJson(data as Map<String, dynamic>));
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增经销商
  addDistributor({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addDistributor, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改经销商
  updateDistributor({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateDistributor, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除经销商
  deleteDistributor({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteDistributor, ids, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //==================================================================================
  /// 经销商协销列表
  distributorAssistList({
    int page = 1,
    int size = pageSize,
    String uId = '',
    bool sortDesc = true,
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "customerInfoId": uId,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
    };
    Request.appGet(
      RequestApi.distributorAssistList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<DistributorAssist>.from(
                resultData['list'].map((e) => DistributorAssist.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增经销商协销记录
  addDistributorAssist({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addDistributorAssist, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除经销商协销记录
  deleteDistributorAssist({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteDistributorAssist, ids, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //==================================================================================
  /// 经销商订单列表
  distributorOrderList({
    int page = 1,
    int size = pageSize,
    String uId = '',
    bool sortDesc = true,
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "customerInfoId": uId,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
    };
    Request.appGet(
      RequestApi.distributorOrderList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;

          success(PaginatedData(
            data: List<DistributorOrders>.from(
                resultData['list'].map((e) => DistributorOrders.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增经销商订单记录
  addDistributorOrder({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addDistributorOrder, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改经销商订单记录
  updateDistributorOrder({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateDistributorOrder, params, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除经销商订单记录
  deleteDistributorOrder({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteDistributorOrder, ids, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //==================================================================================
  /// 经销商参会记录
  distributorConferenceList({
    int page = 1,
    int size = pageSize,
    String uId = '',
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "customerInfoId": uId,
    };
    Request.appGet(
      RequestApi.distributorConferenceList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<DistributorConferences>.from(
                resultData['list'].map((e) => DistributorConferences.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增经销商参会记录
  addDistributorConference({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addDistributorConference, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除经销商参会记录
  deleteDistributorConference({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteDistributorConference, ids, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改经销商参会记录
  updateDistributorConference({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateDistributorConference, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  //==================================================================================
  /// 经销商出库记录
  distributorOutboundList({
    int page = 1,
    int size = pageSize,
    String uId = '',
    bool sortDesc = true,
    // 降序排列
    String order = "createdAt",
    Success<PaginatedData>? success,
    Fail? fail,
  }) {
    Map<String, dynamic> paramMap = {
      "page": page,
      "size": size,
      "customerInfoId": uId,
      "sortDesc": sortDesc ? "desc" : "asc",
      "order": order,
    };
    Request.appGet(
      RequestApi.distributorOutboundList,
      paramMap,
      dialog: false,
      isJson: true,
      success: (data) {
        if (success != null) {
          dynamic resultData = data;
          success(PaginatedData(
            data: List<DistributorOutbounds>.from(
                resultData['list'].map((e) => DistributorOutbounds.fromJson(e))),
            pagination: Pagination.fromJson(resultData['pagination']),
          ));
        }
      },
      fail: (code, msg) {
        fail?.call(code, msg);
      },
    );
  }

  /// 新增经销商出库记录
  addDistributorOutbound({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.addDistributorOutbound, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 删除经销商出库记录
  deleteDistributorOutbound({
    required List<String> ids,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.deleteDistributorOutbound, ids, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  /// 修改经销商出库记录
  updateDistributorOutbound({
    required Map<String, dynamic> params,
    Success? success,
    Fail? fail,
  }) {
    Request.appPost(RequestApi.updateDistributorOutbound, params, isJson: true, success: (data) {
      success?.call(data);
    }, fail: (code, msg) {
      fail?.call(code, msg);
    });
  }

  // /// 检查版本更新
  // checkUpdate({
  //   Success<CheckUpdateInfo>? success,
  //   Fail? fail,
  // }) {
  //   Request.appGet(RequestApi.checkUpdate, {'appId': 'com.xaqinren.healthyelders'},
  //       dialog: false, coverData: true, success: (data) {
  //     success?.call(CheckUpdateInfo.fromJson(data));
  //   }, fail: (code, msg) {
  //     fail?.call(code, msg);
  //   });
  // }
}
